package other.up.modbus.demo;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Random;

public class Test {
	static Enumeration portList;

	static CommPortIdentifier portId;

	static SerialPort serialPort;

	static OutputStream outputStream;

	static InputStream inputStream;
	
	static String nodeIdx="";
	
	static String crc="";
	public static StringBuffer open()throws Exception {
		CommPortIdentifier commportidentifier = CommPortIdentifier
				.getPortIdentifier("COM1");
		serialPort = (SerialPort) commportidentifier.open("scy app", 80);
		outputStream = serialPort.getOutputStream();
		inputStream = serialPort.getInputStream();
		InputStream input = serialPort.getInputStream();
		while (true) {
			StringBuffer content = new StringBuffer();
			int i = 0;
			 String tag = "";
			 String crcCode1 = "";
			 String[] inputString = new String[6];
			 String crcCode2 = "";
			 String tempString = "";
			 serialPort.setInputBufferSize(8);
			 
			while (i < 8) {
				if (i < 6) {
					tempString = Integer.toHexString(input.read());
					if (tempString.length() < 2) {
						tempString = "0" + tempString;
					}
					inputString[i] = tempString;
					// inputString[0];得到站点名称
					nodeIdx = inputString[0];
					// inputString[2] + inputString[3];得到检测指标
					tag = inputString[2] + inputString[3];
					content.append(tempString);
					System.out.println("==这是数据段"+content);
				} else {
					if (i == 6) {// 得到Crc值的前两位
						crcCode1 = Integer.toHexString(input.read());
						if (crcCode1.length() < 2) {
							crcCode1 = "0" + crcCode1;
						}
					}
					if (i == 7) {// 得到Crc值的后两位
						crcCode2 = Integer.toHexString(input.read());
						if (crcCode2.length() < 2) {
							crcCode2 = "0" + crcCode2;
						}
					}
					crc=crcCode1+crcCode2;
					System.out.println("==这是crc"+crc);
				}
				i++;
			}

			if (CRC16.getCrc(content.toString()).equals(crc)) {
				// System.out.println(crcCode1 + crcCode2 + "得到的数据段正确"
				// + CRC16.getCrc(inputString));
				String nodeName = "";
				if (nodeIdx.equals("01")) {
					nodeName = "监测站点1";
					checkNode(tag, nodeName);
				} else {
					System.out.println("这也许是监测站点2的站点");
					nodeName = "监测站点2";
					checkNode(tag, nodeName);
				}
			} else {
				System.out.println(crc + "得到的数据段不正确"
						+ CRC16.getCrc(content.toString()));
			}
			
		}
	}

	public static void main(String[] args) { 
		try {
			open();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void checkNode(String tag, String nodeName) {

		if (tag.equals("012c")) {
			// System.out.println("这是说明要查悬浮物");
			sendData(nodeName, "悬浮物");
		} else if (tag.equals("0064")) {
			// System.out.println("这是说明要查PH值");

			sendData(nodeName, "PH值");
		} else if (tag.equals("0b54")) {
			// System.out.println("这是说明要查氨氮");

			sendData(nodeName, "氨氮");
		} else if (tag.equals("0d48")) {

			sendData(nodeName, "污水总量");
		} else if (tag.equals("0000")) {
			sendData(nodeName, "污水总量");
		} else if (tag.equals("01f4")) {
			sendData(nodeName, "COD");
		} else {
			// System.out.println(tag + "这个tag不知道是啥");
		}
	}

	public static int getRandom(int min, int max) {
		Random random = new Random();
		int s = random.nextInt(max) % (max - min + 1) + min;
		return s;
	}

	public static int getRandomEnd() {
		int max = 99;
		int min = 01;
		Random random = new Random();
		int s = random.nextInt(max) % (max - min + 1) + min;
		return s;
	}

	public static float getFloatNum(int min, int max) {
		return Float.valueOf(getRandom(min, max) + "." + getRandomEnd());
	}

	public static void sendData(String nodeName, String tagName) {
		DB db = new DB();
		try {
			Float ff = 0f;
//			ff = db.onSelect(nodeName, tagName);
			ff=getFloatNum(1,99);
			java.text.NumberFormat formater = java.text.DecimalFormat
					.getInstance();

			String[] totalTemp = new String[7];
			if (nodeName.equals("检测站点1")) {
				totalTemp[0] = "01";
				// if(tagName.equals("COD")){
				// ff=15f;
				// }else{
				// ff=11f;
				// }
			} else {
				totalTemp[0] = "02";
			}
			formater.setMaximumFractionDigits(2);
			formater.setMinimumFractionDigits(2);
			ff = Float.valueOf(formater.format(ff));
			System.out.println("查询完毕," + tagName + "值为:" + ff);
			int fi = Float.floatToRawIntBits(ff);
			String num = Integer.toHexString(fi).toString();
			if (!num.equals("0")) {
				totalTemp[1] = "03";
				totalTemp[2] = "04";
				totalTemp[4] = num.substring(6, 8);
				totalTemp[3] = num.substring(4, 6);
				totalTemp[6] = num.substring(2, 4);
				totalTemp[5] = num.substring(0, 2);

			} else {
				totalTemp[1] = "03";
				totalTemp[2] = "04";
				totalTemp[4] = "00";
				totalTemp[3] = "00";
				totalTemp[6] = "00";
				totalTemp[5] = "00";
			}

			byte[] sendB = CRC16.getSendBuf2(totalTemp);

			outputStream.write(sendB);
			outputStream.flush();
			outputStream.close();
			System.out.println("发送完毕");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

}
